/*
 * Copyright (c) 2024 - 2025 Midea Company of Midea Group.
 *
 * File Name 	    : ui_base_page.c
 * Description	    : 界面管理器
 * Author			: huocy
 * Date	            : 2024/04/12
 * History	        : 
 */

#include "ui_base_page.h"

#define  CFG_ANIM_TIME  (150)

static page_manager_t *cur_manager = NULL;
static list *g_manager_pool = NULL;
static uint32_t nav_bar_tag = 2 << 15;
/*通过名字在管理器列表中找到管理器*/
page_manager_t *find_manager_pool(const char *manager_name)
{
    listIter *iter = listGetIterator(g_manager_pool, AL_START_HEAD);
    for (listNode *node = listNext(iter); node != NULL; node = listNext(iter))
    {
        if (strcmp(((page_manager_t *)node->value)->manager_name, manager_name) == 0)
        {
            //PM_LOG_INFO("find page_manager_t %s addr[%p]\n", manager_name, node);
            listReleaseIterator(iter);
            return (page_manager_t *)node->value;
        }
    }
    listReleaseIterator(iter);
    return NULL;
}

/*第一步初始化页面管理*/
void page_init(char *manager_name)
{
    if (g_manager_pool == NULL)
    {
        g_manager_pool = listCreate();
    }

    cur_manager = find_manager_pool(manager_name);
    if (cur_manager == NULL)
    {
        cur_manager = pm_create();
        cur_manager->manager_name = manager_name;
        listAddNodeTail(g_manager_pool, cur_manager);
    }
    else
    {
        //PM_LOG_INFO(" page_manager_t %s is already exsit \n", manager_name);
    }

    pm_set_global_load_anim_type(cur_manager, LOAD_ANIM_OVER_LEFT, CFG_ANIM_TIME, lv_anim_path_linear);
}

/*第二步安装对应页面到页面池*/
void page_install(const char *name, page_vtable_t *pagev,void* priv_data)
{
    if (pagev != NULL)
    {
        pm_install(cur_manager, name, pagev,priv_data);
    }
    else
    {
        PM_LOG_ERROR("page not exist");
    }
}

void page_uninstall(const char *name)
{
    if (name != NULL)
    {
        pm_uninstall(cur_manager,name);
    }
    else
    {
        PM_LOG_ERROR("page not exist");
    }
}

void page_satck_clear(bool keep_bottom)
{
    set_satck_clear(cur_manager,keep_bottom);
}


/*第三步push对应页面进栈容器池*/
void page_push(const char *name)
{
    //PM_LOG_INFO("page push next page");
    pm_push(cur_manager, name, NULL);
}

void page_push_anim(const char *name,page_load_anim_t anim)
{
    //PM_LOG_INFO("page push next page");
    pm_set_global_load_anim_type(cur_manager, anim, CFG_ANIM_TIME, lv_anim_path_linear);
    cur_manager->anim_state.current.type = anim;
    pm_push(cur_manager, name, NULL);
}

void page_push_with_stash(const char *name, const page_stash_t *stash){
   pm_push(cur_manager, name, stash);
}


/*push对应页面进栈容器池带完成回调，显示*/
void page_push_complete(const char *name, view_navigator_finsh complete)
{
    //PM_LOG_INFO("page push next page");
    pm_push_compte(cur_manager, name, NULL, complete);
}

/*pop应页面出栈容器池，退出显示*/
void page_pop(void)
{
    //PM_LOG_INFO("page pop last page");
    pm_pop(cur_manager);
}

int page_is_runing_anim(void)
{
    if(!cur_manager)
        return 0;

    if(pm_is_anim_runing(cur_manager))
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

void page_pop_anim(page_load_anim_t anim)
{
    //PM_LOG_INFO("page pop last page");
    pm_set_global_load_anim_type(cur_manager, anim, CFG_ANIM_TIME, lv_anim_path_linear);

    cur_manager->anim_state.current.type = anim;

    pm_pop(cur_manager);
}

void page_pop_anim_name(page_load_anim_t anim,const char *name)
{
    //PM_LOG_INFO("page pop last page");
    pm_set_global_load_anim_type(cur_manager, anim, CFG_ANIM_TIME, lv_anim_path_linear);

    cur_manager->anim_state.current.type = anim;

    pm_pop_page(cur_manager,name);
}

  

/*install和push和pop快捷操作接口*/
void page_view(const char *page_name, page_vtable_t *pagev)
{
    //if (find_page_pool(cur_manager, page_name) == NULL)
    if (!find_page_pool(cur_manager, page_name))
    {
        page_install(page_name, pagev,NULL);
    }

    //if (find_page_stack_node(cur_manager, page_name) != NULL)
    if (find_page_stack_node(cur_manager, page_name))
    {
        page_popToPage(page_name);
    }
    else
    {
        page_push(page_name);
    }
}

// 新page_name页面显示,并将stack容器中栈底到page_name之间的页面删除，即page_name为根页面
void page_switch_new_root(const char *page_name)
{
    pm_switch_new_root(cur_manager, page_name);
    //PM_LOG_INFO("page switch renew root page");
}

/*pop出栈 直到只有一个页面在stack中。即销毁statck中其他的页面，pop回根页面显示*/
void page_popToRoot(void)
{
    pm_back_home(cur_manager);
    PM_LOG_INFO("page pop to root page");
}

/*pop出栈，直到指定的页面在栈顶。即销毁stack容器中，栈顶到page_name之间的页面，pop到page_name页面显示。*/
void page_popToPage(char *page_name)
{
    pm_pop_page(cur_manager, page_name);
    //PM_LOG_INFO("page pop to page %s ", page_name);
}

/*从栈容器中清除指定page，包括page_pool和stack_pool中的对应元素。page_name不可为当前栈顶。*/
void page_delete_page(char *page_name)
{
    pm_delete_page(cur_manager, page_name);
    PM_LOG_INFO("page delete page %s ", page_name);
}

/*切换页面管理器*/
page_manager_t *page_switch_manager(char *manager_name)
{
    page_manager_t *manager = find_manager_pool(manager_name);
    if (manager == NULL)
    {
        //PM_LOG_INFO(" page_manager_t %s is not exsit \n", manager_name);
        return NULL;
    }
    cur_manager = manager;
    return manager;
}

/*删除页面管理器对象。包含page_pool和page_pool中所有元素。*/
void page_delete_manager(char *manager_name)
{
    page_manager_t *manager = find_manager_pool(manager_name);
    if (manager == NULL)
    {
        return;
    }

    pm_delete_manager(manager);
    listDelNode(g_manager_pool, listSearchKey(g_manager_pool, manager));
    //PM_LOG_INFO(" page manager list clear ");
}

/*跳转前 自定义参数事件*/
void on_base_custom_attr_config(page_base_t *self)
{
    //PM_LOG_INFO("on_custom_attr_config");
}

/* 页面 加载事件 */
void on_base_view_load(page_base_t *self)
{
    //PM_LOG_INFO("on_view_load");
}

int nav_bar_click_cb(lv_obj_t *navbar, lv_obj_t *taget, uint8_t index)
{
    printf("nav_bar click back %d\n", index);
    if (index == 0)
    {
        page_pop();
    }

    return 0;
}
/* 页面加载完成事件 */
void on_base_view_did_load(page_base_t *self)
{
    //PM_LOG_INFO("on_view_did_load");
    // lv_obj_t *nav = lv_midea_navbar_create(self->root, "");
    lv_obj_set_style_bg_color(self->root, lv_color_hex(0x000000), 0);
    // self->nav_bar = nav;

    // lv_midea_navbar_event_cb(nav, nav_bar_click_cb);
}

/* 页面即将展示事件 */
void on_base_view_will_appear(page_base_t *self)
{
    //PM_LOG_INFO("on_view_will_appear");
}

/* 页面已经展示事件 */
void on_base_view_did_appear(page_base_t *self)
{
    //PM_LOG_INFO("on_view_did_appear");
}

/* 页面即将消失事件 */
void on_base_view_will_disappear(page_base_t *self)
{
    //PM_LOG_INFO("on_view_will_disappear");
}

/* 页面消失完成事件 */
void on_base_view_did_disappear(page_base_t *self)
{
    //PM_LOG_INFO("on_view_did_disappear");
}

/* 页面卸载完成事件 */
void on_base_view_did_unload(page_base_t *self)
{
    //PM_LOG_INFO("on_view_did_unload");
}

/*返回上一页*/
static void popBack(lv_event_t *e)
{
    if (e->code == LV_EVENT_CLICKED)
    {
        page_pop();
        //PM_LOG_INFO("导航pop 到上一个页面");
    }
}

